package com.ysd.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ysd.entity.LeaveMessages;
import com.ysd.service.LeaveMessagesService;
import com.ysd.util.Result;

@Controller
public class LeaveMessageController {
	@Autowired
	private LeaveMessagesService leaveMessagesService;
	@RequestMapping("addMessage")
	@ResponseBody
	public Object addMessage(@Valid LeaveMessages leaveMessages,BindingResult bindingResult) {
		System.out.println(bindingResult.hasErrors());
		if(bindingResult.hasErrors()) {
			return	Result.toClient("1", "添加失败", null,null);
		}else {
			int n = leaveMessagesService.addMessages(leaveMessages);
			if(n>0) {
				return	Result.toClient("0", "添加成功", null,null);
			}else {
				return	Result.toClient("1", "添加失败", null,null);
			}
		}

	}
	

@RequestMapping("getMessages")
	@ResponseBody
	public Object getMessages(LeaveMessages leaveMessages) {
		leaveMessages.setPage((leaveMessages.getPage()-1)*leaveMessages.getLimit());
		List<LeaveMessages>list = leaveMessagesService.getMessages(leaveMessages);
		int count = leaveMessagesService.getCount(leaveMessages);
		if (list != null) {
			return Result.toClient("0", "查询成功", count, list);
		} else {
			return Result.toClient("1", "查询失败", count, list);
		}
	}
	
	@RequestMapping("updateMessages")
	@ResponseBody
	public Object updateMessages(LeaveMessages leaveMessages) {
		int n = leaveMessagesService.updateMessages(leaveMessages);
		if (n > 0) {
			return Result.toClient("0", "修改成功");
		} else {
			return Result.toClient("1", "修改失败");
		}
	}


}
